<?php

namespace App\Service\SiteLocation\Import;

use App\Exceptions\Custom\ResponseHttpException;
use App\Exceptions\KfHttpResponseException;
use App\Models\Common\MapNationalBrandShopModel;
use App\Models\OpenShop\Common\MallSettleInShopInfoModel;
use App\Models\Wanggui\WangguiDotYhatModel;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;

class WgPrediction
{
    public function collection(Collection $collection, ?Command $command)
    {
        $num = 0;
        $collection->first()->map(function ($dot) use (&$num) {
            if ($num == 0) {
                $header = [
                    "wgNumber",
                    "date",
                    "delivery_yhat_lower",
                    "delivery_yhat_upper",
                    "delivery_yhat",
                    "dine_in_yhat_lower",
                    "dine_in_yhat_upper",
                    "dine_in_yhat",
                    "total_yhat_lower",
                    "total_yhat_upper",
                    "total_yhat",
                ];
                foreach ($header as $key => $value) {
                    if ($dot[$key] != $value) {
                        return;
                    }
                }
                $num++;
                return;
            }

            if ($dot[0] == null) {
                return;
            }


            $model = WangguiDotYhatModel::query()->firstOrNew([
                'wgNumber' => $dot[0],
                'date'     => Carbon::createFromDate($dot[1])->timestamp
            ]);

            if (!$model->exists) {
                // 手动保存新实例到数据库
                $model->save();
            }
            $model->delivery_yhat_lower = $dot[2];
            $model->delivery_yhat_upper = $dot[3];
            $model->delivery_yhat       = $dot[4];
            $model->dine_in_yhat_lower  = $dot[5];
            $model->dine_in_yhat_upper  = $dot[6];
            $model->dine_in_yhat        = $dot[7];
            $model->total_yhat_lower    = $dot[8];
            $model->total_yhat_upper    = $dot[9];
            $model->total_yhat          = $dot[10];
            $model->save();
            dump($num);
            $num++;
        });
    }
}
